package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.Meter;
import com.ruoyi.system.domain.MeterRecord;
import com.ruoyi.system.domain.vo.MeterVO;
import com.ruoyi.system.service.IMeterRecordService;
import com.ruoyi.system.service.IMeterService;
import com.ruoyi.system.service.ISysConfigService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 电Controller
 *
 * @author ruoyi
 * @date 2021-10-28
 */
@Api("电表管理")
@RestController
@RequestMapping("/system/meter")
public class MeterController extends BaseController
{
    @Autowired
    private IMeterService meterService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IMeterRecordService meterRecordService;

    /**
     * 查询电列表
     */
    @ApiOperation(" 查询电列表 ")
    @GetMapping("/list")
    public TableDataInfo list(Meter meter)
    {
        startPage();
        List<MeterVO> list = meterService.selectMeterList(meter);
        return getDataTable(list);
    }
    @ApiOperation(" 查询电列表 无权限")
    @GetMapping("/listNoAuth")
    public TableDataInfo listNoAuth(Meter meter)
    {
        startPage();
        List<MeterVO> list = meterService.selectMeterList(meter);
        return getDataTable(list);
    }

    /**
     * 导出电列表
     */
//    @Log(title = "电", businessType = BusinessType.EXPORT)
//    @GetMapping("/export")
//    public AjaxResult export(Meter meter)
//    {
//        List<Meter> list = meterService.selectMeterList(meter);
//        ExcelUtil<Meter> util = new ExcelUtil<Meter>(Meter.class);
//        return util.exportExcel(list, "电数据");
//    }

    /**
     * 获取电详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(meterService.selectMeterById(id));
    }

    /**
     * 新增电
     */
    @ApiOperation(" 新增电列表 ")
    @PostMapping
    public AjaxResult add(@RequestBody Meter meter)
    {
    	meter.setPhone("15618975192");
    	meter.setTenantId("admin");
        return toAjax(meterService.insertMeter(meter));
    }

    @Transactional
    @ApiOperation(" 新增电列表 ")
    @PostMapping("addMore")
    public AjaxResult addMore(@RequestBody List<Meter> meterLsit)
    {
    	if (CollectionUtils.isNotEmpty(meterLsit)) {
			for (Meter meter2 : meterLsit) {
				meter2.setPhone("15618975192");
		    	meter2.setTenantId("admin");
				meterService.insertMeter(meter2);
			}
		}
        return AjaxResult.success();
    }

    /**
     * 修改电
     */
    @ApiOperation(" 修改电列表 ")
    @PutMapping
    public AjaxResult edit(@RequestBody Meter meter)
    {
    	meter.setMeterValue(null);
    	meter.setMeterWarn(null);
        return toAjax(meterService.updateMeter(meter));
    }



    @ApiOperation(" 充值电列 ")
    @PutMapping("addRecharge")
    @Transactional
    public AjaxResult recharge(@RequestBody MeterVO meter)
    {
    	Long id = meter.getId();
    	if (id == null ) {
			return AjaxResult.error("id没传");
		}
    	BigDecimal chargeValue = meter.getChargeValue();
    	if (chargeValue == null || chargeValue.compareTo(BigDecimal.ZERO) < 0) {
    		return AjaxResult.error("没传充值金额");
		}
    	Meter selectMeterById = meterService.selectMeterById(id);
    	BigDecimal meterWarn = selectMeterById.getMeterWarn();
    	if (meterWarn == null ) {
			meterWarn = BigDecimal.ZERO;
		}
        Meter meterNew = new Meter();
        meterNew.setId(id);
        String selectConfigByKey = sysConfigService.selectConfigByKey("sys.dianbiao.price");
        BigDecimal divide = chargeValue.divide(new BigDecimal(selectConfigByKey),2,BigDecimal.ROUND_HALF_DOWN);
        meterNew.setMeterWarn(meterWarn.add(divide));

        MeterRecord meterRecord = new MeterRecord();
        meterRecord.setMeterConfig(new BigDecimal(selectConfigByKey));
        meterRecord.setMeterId(id);
        meterRecord.setAddMeterWarn(divide);
        meterRecord.setRechargeValue(chargeValue);
		meterRecordService.insertMeterRecord(meterRecord );
		return toAjax(meterService.updateMeter(meterNew ));
    }
//    public static void main(String[] args) {
//		System.err.println(new BigDecimal(1).divide(new BigDecimal(3),2,BigDecimal.ROUND_HALF_DOWN));
//	}

    @ApiOperation(" 修改电列表 没权限")
    @PutMapping("updateNoAuth")
    public AjaxResult editNoAuth(@RequestBody Meter meter)
    {
    	meter.setMeterValue(null);
    	return toAjax(meterService.updateMeter(meter));
    }

    @ApiOperation(" 修改电权限 没权限 传id  和 status  1开启  2关闭")
    @PutMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody Meter meter)
    {
    	Long id = meter.getId();
    	if (id == null ) {
			return  AjaxResult.error("id没传");
		}
    	Integer status = meter.getStatus();
    	if (status == null ) {
			return  AjaxResult.error("status没传");
		}

    	Meter meterNew = new Meter();
    	meterNew.setId(id);
    	meterNew.setStatus(status);
		return toAjax(meterService.updateMeter(meterNew ));
    }



    @Transactional
    @ApiOperation(" 修改电列表多个 没权限")
    @PutMapping("updateNoAuthMoreDetail")
    public AjaxResult updateNoAuthMoreDetail(@RequestBody List<Meter> meterLsit)
    {
    	if (CollectionUtils.isNotEmpty(meterLsit)) {
    		for (Meter meter2 : meterLsit) {
				if(meter2.getId() == null ) return AjaxResult.error("没传id 参数");
				meter2.setMeterValue(null);
				meter2.setMeterWarn(null);
			}
    		for (Meter meter2 : meterLsit) {
    			meterService.updateMeter(meter2);
    		}

    	}
    	return AjaxResult.success();
    }


    @Transactional
    @ApiOperation(" 修改电列表多个 没权限")
    @PutMapping("updateNoAuthMore")
    public AjaxResult updateNoAuthMore(@RequestBody List<Meter> meterLsit)
    {
    	if (CollectionUtils.isNotEmpty(meterLsit)) {
    		for (Meter meter2 : meterLsit) {
				if(meter2.getId() == null ) return AjaxResult.error("没传id 参数");
				BigDecimal meterValue = meter2.getMeterValue();
				Meter selectMeterById = meterService.selectMeterById(meter2.getId());
				if (selectMeterById != null ) {
					BigDecimal meterValueOld = selectMeterById.getMeterValue();
					if (meterValueOld == null ) {
						meterValueOld = BigDecimal.ZERO;
					}
					if (meterValueOld.compareTo(meterValue) > 0) {
						 return AjaxResult.error("存在电表度数小于以前值");
					}
				}

			}
    		for (Meter meter2 : meterLsit) {
    			meterService.updateMeter(meter2);
    		}

    	}
    	return AjaxResult.success();
    }

    /**
     * 删除电
//     */
//    @PreAuthorize("@ss.hasPermi('system:meter:remove')")
//    @Log(title = "电", businessType = BusinessType.DELETE)
//	@DeleteMapping("/{ids}")
//    public AjaxResult remove(@PathVariable Long[] ids)
//    {
//        return toAjax(meterService.deleteMeterByIds(ids));
//    }
}
